CSS Full Course Day 7 [Hindi] π» | Positioning (Static, Relative, Absolute, Fixed) π | Mohit Decodes
π CSS Tutorial β Day 7: Positioning (Static, Relative, Absolute, Fixed)
Welcome to Day 7 of the CSS Full Course [Hindi] by Mohit Decodes! Todayβs lesson focuses on CSS Positioning β how you control the exact placement of elements on a web page.
πΉ Types of CSS Positioning
- Static (Default)
- Elements follow the normal flow of the page
- No special positioning applied
- Default value for all elements
- Relative
- Positioned relative to its normal position
- Use
top
,bottom
,left
,right
to move it around without affecting other elementsβ positions - Absolute
- Positioned relative to the nearest positioned ancestor (
relative
,absolute
, orfixed
) - Removed from normal flow, does not affect other elements
- Use
top
,bottom
,left
,right
for exact placement - Fixed
- Positioned relative to the browser window
- Stays fixed even when scrolling
- Common for sticky headers or floating buttons
βοΈ Example CSS:
css
CopyEdit
.static-box {
position: static;
background-color: lightgray;
}
.relative-box {
position: relative;
top: 10px;
left: 20px;
background-color: lightblue;
}
.absolute-box {
position: absolute;
top: 50px;
right: 30px;
background-color: lightgreen;
}
.fixed-box {
position: fixed;
bottom: 10px;
right: 10px;
background-color: lightcoral;
width: 150px;
padding: 10px;
color: white;
}
π‘ Tips:
- Use relative positioning for slight adjustments without disrupting layout
- Absolute positioning is great for overlays, modals, or tooltips
- Fixed is useful for sticky navigation bars or floating action buttons
- Always consider responsiveness and stacking context (
z-index
)